package com.pachong.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.common.core.utils.CopyUtil;
import com.common.core.utils.TreeUtil;
import com.domain.bean.sys.SysMenu;
import com.domain.bean.sys.SysRole;
import com.domain.bean.dto.SysMenuVO;
import com.domain.bean.dto.SysRoleVO;
import com.pachong.common.impl.BaseServiceImpl;
import com.pachong.service.SysMenuService;
import com.wy.dao.sys.BaseDAO;
import com.wy.dao.sys.SysMenuDAO;
import com.wy.dao.sys.SysRoleDAO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Author : wangyu
 * Date: 2018-01-26
 * Time: 15:39
 */
@Service
public class SysMenuServiceImpl extends BaseServiceImpl<SysMenu,String> implements SysMenuService {
    private static Logger LOGGER= LoggerFactory.getLogger(SysMenuServiceImpl.class);

    @Autowired
    private SysMenuDAO sysMenuDAO;
    @Autowired
    private SysRoleDAO sysRoleDAO;

    @Override
    public BaseDAO<SysMenu, String> getBaseDao() {
        return sysMenuDAO;
    }

    @Override
    public JSONArray getMenuJsonByUser(List<SysMenuVO> menuList) {
        //List<SysMenu> menuListOne=new ArrayList<>();//获取第一级别
        JSONArray jsonArr=new JSONArray();
        menuList.sort((sysMenu1,sysMenu2) ->{
            return 0;
        });
        Collections.sort(menuList, new Comparator<SysMenuVO>() {
            @Override
            public int compare(SysMenuVO o1, SysMenuVO o2) {
                if(o1.getOrderNum()==null||o2.getOrderNum()==null){
                    return -1;
                }
                if(o1.getOrderNum() > o2.getOrderNum()){
                    return 1;
                }
                if(o1.getOrderNum()==o2.getOrderNum()){
                    return 0;
                }
                return -1;
            }
        });
        int pNum=1000;
        for(SysMenuVO menu:menuList){
            if(StringUtils.isEmpty(menu.getPId())){
                SysMenuVO sysMenu=getChilds(menu,pNum,0,menuList);
                jsonArr.add(sysMenu);
                pNum+=1000;
            }
        }
        return jsonArr;
    }

    public SysMenuVO getChilds(SysMenuVO menu, int pNum, int num, List<SysMenuVO> menuList){
        for(SysMenuVO menus:menuList){
            if(menu.getId().equals(menus.getPId())&&menus.getMenuType()==0){
                ++num;
                SysMenuVO m=getChilds(menus,pNum,num,menuList);
                m.setNum(pNum+num);
                menu.addChild(m);
            }
        }
        return menu;
    }

    @Override
    @Transactional
    public List<SysMenuVO> getUserMenu(String userId) {
        //把javabean转换成dto对象
        List<SysMenu>  userMenus = sysMenuDAO.getUserMenu(userId);

        //list对象转换
        List<SysMenuVO> sysMenuVOS = userMenus.stream().map(sm -> {
            SysMenuVO sysMenuVO = CopyUtil.copyObject(sm, SysMenuVO.class);
            Set<SysRoleVO> sysRoleVOS = CopyUtil.copySet(sm.getRoles(), SysRoleVO.class);
            sysMenuVO.setRoleList(sysRoleVOS);
            return sysMenuVO;
        }).collect(Collectors.toList());;
        return sysMenuVOS;
    }

    @Override
    public JSONArray getTreeUtil(String roleId) {
        TreeUtil treeUtil=null;
        List<SysMenu> mList=sysMenuDAO.getMenuNotSuper();
        JSONArray jsonArr=new JSONArray();
        for(SysMenu sysMenu:mList){
            treeUtil=getChildByTree(sysMenu.getId(),false,0,null,roleId);
            jsonArr.add(treeUtil);
        }
        return jsonArr;
    }

    @Override
    public List<SysMenu> getMenuNotSuper() {
        return sysMenuDAO.getMenuNotSuper();
    }

    @Override
    public List<SysMenu> getMenuChildren(String id) {
        return sysMenuDAO.getMenuChildren(id);
    }

    @Override
    public List<SysMenuVO> getMenuVoList() {
        List<SysMenu> mList=sysMenuDAO.getMenuNotSuper();
        List<SysMenuVO> sysMVS = new ArrayList<>();
        for(SysMenu sysMenu:mList){
            SysMenuVO menu=getChild(sysMenu.getId(),false,0,0);
            sysMVS.add(menu);
        }
        return sysMVS;
    }

    public TreeUtil getChildByTree(String id,boolean flag,int layer,String pId,String roleId){
        layer++;
        SysMenu sysMenu=this.selectByPrimaryKey(id);
        List<SysMenu> mList=null;
        if(flag){
            mList= sysMenuDAO.getMenuChildren(id);
        }else{
            mList=sysMenuDAO.getMenuChildrenAll(id);
        }
        TreeUtil treeUtil = new TreeUtil();
        treeUtil.setId(sysMenu.getId());
        treeUtil.setName(sysMenu.getName());
        treeUtil.setLayer(layer);
        treeUtil.setPId(pId);
        /**判断是否存在*/
        if(!StringUtils.isEmpty(roleId)){
            SysRole role = sysRoleDAO.findOne(roleId);
            Set<SysMenu> menus = role.getMenus();
            if(menus != null && !menus.isEmpty() ){
                for(SysMenu menu:role.getMenus()){
                    if(menu.getId().equals(id))
                        treeUtil.setChecked(true);
                }
            }
        }

        for(SysMenu menu:mList){
            TreeUtil m=getChildByTree(menu.getId(),flag,layer,menu.getId(),roleId);
            treeUtil.getChildren().add(m);
        }
        return treeUtil;
    }

    /**
     *
     * @param id 父菜单id
     * @param flag true 获取非按钮菜单 false 获取包括按钮在内菜单 用于nemuList展示
     * @param pNum 用户控制侧拉不重复id tab 父循环+1000
     * @param num 用户控制侧拉不重复id tab 最终效果 1001 10002 2001 2002
     * @return
     */
    public SysMenuVO getChild(String id, boolean flag, int pNum, int num){
        SysMenu sysMenu=this.selectByPrimaryKey(id);
        SysMenuVO smt = CopyUtil.copyObject(sysMenu,SysMenuVO.class);

        List<SysMenu> mList=null;
        if(flag){
            mList= sysMenuDAO.getMenuChildren(id);
        }else{
            mList=sysMenuDAO.getMenuChildrenAll(id);
        }

        for(SysMenu menu:mList){
            ++num;
            SysMenuVO m=getChild(menu.getId(),flag,pNum,num);
            if(flag)
                m.setNum(pNum+num);
            smt.addChild(m);
        }
        return smt;
    }
}
